package com.example.controller;

import cn.hutool.json.JSONUtil;
import com.example.common.HttpStatusEnum;
import com.example.entity.ResponseData;
import com.example.pojo.Disease;
import com.example.service.DiseaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 疾病信息表(Disease)表控制层
 *
 * @email: pengyujun53@163.com
 * @author: peng_YuJun
 * @date: 2024/01/17
 * @time: 23:13
 */
@RestController
@RequestMapping("disease")
@Api(tags = "Disease信息请求类")
public class DiseaseController {
    /**
     * 服务对象 
     */
    @Resource
    private DiseaseService diseaseService;

    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @param disease 筛选条件
     * @return 查询结果
     */
    @ApiOperation("分页查询")
    @GetMapping("getPage")
    public ResponseData getPage(Integer currentPage, Integer pageSize, Disease disease) {
        if (currentPage == null || pageSize == null){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(), HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return diseaseService.getByPage(currentPage, pageSize, disease);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("根据ID查询信息")
    @GetMapping("findById")
    public ResponseData findById(Long id) {
        if (id == null){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(),HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return diseaseService.findById(id);
    }

    /**
     * 新增数据
     *
     * @param  disease 实体
     * @return 新增结果
     */
    @ApiOperation("增加")
    @PostMapping("add")
    public ResponseData add(Disease disease) {
        if(disease == null){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(), HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return diseaseService.add(disease);
    }

    /**
     * 更新数据
     *
     * @param disease 实体
     * @return 更新结果
     */
    @ApiOperation("更新")
    @PostMapping("update")
    public ResponseData update(Disease disease) {
        if(disease == null || disease.getId() == null){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(), HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return diseaseService.update(disease);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 
     */
    @ApiOperation("删除")
    @PostMapping("delete")
    public ResponseData delete(Long id) {
        if (id == null){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(), HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return diseaseService.delete(id);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation("批量删除")
    @PostMapping("deleteBatch")
    public ResponseData deleteBatch(String ids){
        List<Long> idList = JSONUtil.toList(ids, Long.class);
        if (idList == null || idList.size()<=0){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(), HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return diseaseService.deleteBatch(idList);
    }

    /**
     * 查询有效的疾病信息
     * @return
     */
    @ApiOperation("查询有效的疾病信息")
    @GetMapping("findAllValid")
    public ResponseData findAllValid() {
        return diseaseService.findAllValid();
    }

    /**
     * 统计疾病数
     * @return
     */
    @ApiOperation("统计疾病数")
    @GetMapping("countAll")
    public ResponseData countAll(){
        return diseaseService.countAll();
    }
}

